/**
 * [OElove] (C)2010-2099 oelove.com Inc. biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/02/15 by OE $
*/
var boundx, boundy, flip_obj;
$(function(){
	//上传头像
	$(document).on("change", "[f='but_upload_avatar']", function(){
		$("#upload_avatar_loading").show();
		//上传照片
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?m=user&c=index&a=upload&module=tmp&forbidwater=1&r="+get_rndnum(8),
			"secureuri": false,
			"fileElementId": $(this).attr("id"),
			"dataType": "json",
			"data": {uploadpart: $(this).attr("id")},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$uploadimg = $json.uploadimg;
				$base64img = $json.base64img;
				if ($response == "1") { //成功
					$("#upload_avatar_loading").hide();
					//载入裁剪框
					$.ajax({
						type: "POST",
						url: _ROOT_PATH+"index.php?m=user&c=avatar",
						cache: false,
						data: {
							imgcode:$base64img, datatype:"json", r:get_rndnum(8)
						},
						dataType: "json",
						success: function($a_data) {
							$a_json = eval($a_data);
							$a_response = $a_json.response;
							$a_result = $a_json.result;
							if ($a_response == "1") {
								$("#avatar_box").html("");
								$("#avatar_box").html($a_result);
								$("#avatar_box").fadeIn(200);


								//加载裁剪框
								$("#clip_img_preview").cropper({
									aspectRatio:110/135,
									width: 130,
									height: 169,
									mouseWheelZoom:false,
									touchDragZoom:false,
									crop: function(data) {
										$_x = data.x; //剪裁区域左侧的偏移
										$_y = data.y; //剪裁区域距上部的偏移
										$_cut_width = data.width; //剪裁区域的宽度
										$_cut_height = data.height; //剪裁区域的高度
										$("#x1").val($_x);
										$("#y1").val($_y);
										$("#w").val($_cut_width);
										$("#h").val($_cut_height);
									}
								});


							}
							else {
								if ($a_result.length > 0) {
									ToastShow($a_result);
								}
								else {
									ToastShow("获取数据失败");
								}
							}
						},
						error: function() {
							ToastShow("系统繁忙，请稍后再试");
						}
					});

					
				}
				//失败
				else {
					$error = "上传失败，请检查照片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					ToastShow($error);
				}

			},
			error: function ($data, e, b) {
				ToastShow("上传失败，请检查照片大小和格式！");
			}
		});

	});

	//关闭裁剪框
	$(document).on("click", ".clipbox_img_close", function(){
		$("#avatar_box").hide();
		$("#avatar_box").html("");
	});

	//确定裁剪 JSON
	$(document).on("click", "[f='but_confirm_cut']", function(){
		$obj_cliptips = $("#clip_tips");
		$obj_cliptips.html("");
		$_imgcode = $("#imgcode").val();
		$_x1 = $("#x1").val();
		$_y1 = $("#y1").val();
		$_x2 = $("#x2").val();
		$_y2 = $("#y2").val();
		$_w = $("#w").val();
		$_h = $("#h").val();
		$_boundwidth = boundx; //压缩比例宽
		$_boundheight = boundy; //压缩比例高
		if ($_w.length == 0) {
			$obj_cliptips.html("请拖动设置白色裁剪框");
			return false;
		}
		if ($_h.length == 0) {
			$obj_cliptips.html("请拖动设置白色裁剪框");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=avatar",
			cache: false,
			data: {a:"cropper", imgcode:$_imgcode, x1:$_x1, y1:$_y1, x2:$_x2, y2:$_y2, w:$_w, h:$_h, boundwidth:$_boundwidth, boundheight:$_boundheight, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
					$obj_cliptips.html("<font color='green'>裁剪成功</font>");
					_oeloveFileSynYun($attid); //同步云存储
					setTimeout(function(){
						$("#avatar_box").html("");
						window.location.reload();
					}, 500);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("裁剪失败");
					}
				}
			},
			error: function() {
			}
		});


	});
	
});